﻿using System;
using System.Text;
using RabbitMQDemo.Common;

namespace RabbitMQDemo.Producer
{
    public class Worker
    {
        private const string QueueName = "worker_queue";

        public static void Send(string[] args)
        {
            using var connection = MQClient.GetConnection();
            using var channel = connection.CreateModel();

            // 声明队列
            channel.QueueDeclare(queue: QueueName, durable: false, exclusive: false, autoDelete: false, arguments: null);

            var index = 0;
            while (index < 10)
            {
                // 发送消息
                channel.BasicPublish(exchange: "", routingKey: QueueName, mandatory: false, basicProperties: null, body: GetMessage(index));

                // Thread.Sleep(1000);
                Console.WriteLine("Send worker message");
                index++;
            }
        }

        private static byte[] GetMessage(int? i = null) => Encoding.UTF8.GetBytes($"[Worker]{(i.HasValue ? $"[{i}]" : string.Empty)} message");
    }
}
